/**
 * Created by red on 2017/4/30.
 */
import { parse } from 'utils/';

/**
 * 用户模块
 * @type {Object}
 */
export default [
    {
        name: '登录',
        method: 'login',
        type: 'parse',
        excute: ({ data, fn, errFn } = {}) => {
            parse.login(data.username, data.password).subscribe(res => fn({ ...res.attributes, token: res.attributes.sessionToken }), errFn);
        }
    },
    {
        name: '注册',
        method: 'register',
        type: 'parse',
        excute: ({ data, fn, errFn } = {}) => {
            parse.register(data.username, data.password).subscribe(fn, errFn);
        }
    },
    {
        name: '获取用户信息',
        method: 'findUser',
        type: 'parse',
        excute: ({ data, fn, errFn } = {}) => {
            parse.userInfo().subscribe(fn, errFn);
        }
    },
    {
        name: '获取会员列表',
        method: 'wxUserList',
        type: 'parse',
        excute: ({ data, fn, errFn } = {}) => {
            parse.query(
              'WxUser',
              query => {
                query.count().then(res => data.page.total = res, err => data.page.total = 0);
                query.skip((data.page.current_page - 1) * data.page.page_size);
                query.limit(data.page.page_size);
                if(!!data.params.nickname) query.contains('nickname', data.params.nickname);
              }
            ).filter(x => x.type && x.type === 'result')
              .map(x => x.result)
              .subscribe(fn, errFn);
          }
    }
];